package business_opportunity

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	businessOpportunityPb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.myscrm.cn/golang/stark/v4"
)

type contactRepository struct {
}

// NewContactRepository 联系人
func NewContactRepository() ContactRepositoryIface {
	return &contactRepository{}
}

func (r *contactRepository) GetContactList(ctx context.Context, request *businessOpportunityPb.ContactListRequest) (*businessOpportunityPb.ContactListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewContactServiceClient(conn)
	return client.ContactList(ctx, request)
}

func (r *contactRepository) ContactDetail(ctx context.Context, request *businessOpportunityPb.ContactDetailRequest) (*businessOpportunityPb.ContactDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewContactServiceClient(conn)
	return client.ContactDetail(ctx, request)
}

func (r *contactRepository) CooperationList(ctx context.Context, request *businessOpportunityPb.CooperationListRequest) (*businessOpportunityPb.CooperationListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewContactServiceClient(conn)
	return client.CooperationList(ctx, request)
}

/*
*
获取手机号
*/
func (r *contactRepository) GetMobile(ctx context.Context, request *businessOpportunityPb.GetMobileRequest) (*businessOpportunityPb.GetMobileResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewContactServiceClient(conn)
	return client.GetMobile(ctx, request)
}

func (r *contactRepository) CompanyContactCnt(ctx context.Context, request *businessOpportunityPb.CompanyContactCntRequest) ([]*businessOpportunityPb.CompanyContactCntList, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := businessOpportunityPb.NewContactServiceClient(conn)
	resp, err := client.CompanyContactCnt(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CompanyContactCnt request: %+v, err:%s", request, err.Error())
		return nil, err
	}
	if resp == nil {
		return nil, nil
	}

	return resp.List, nil
}
